import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        ArrayList<ValObj> list = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            list.add(new ValObj("name-" + String.valueOf(i), i));
        }

        list.forEach(System.out::println);
        System.out.println("--------------------------------");

        ValObj obj = new ValObj("name-2-new", 2);

        boolean added = list.add(obj);
        if (added) {
            list.sort((o1, o2) -> Integer.compare(o1.val(), o2.val()));
        } else {
            System.out.println("添加失败");
        }
        list.forEach(System.out::println);
        System.out.println("---------------------------------");

        for (int i = 0; i < list.size(); i++) {
            list.get(i).val = i + 1;
        }
        list.forEach(System.out::println);
        System.out.println("---------------------------------");

    }
}

class ValObj {
    String name;
    Integer val;

    ValObj(String name, Integer val) {
        this.name = name;
        this.val = val;
    }

    Integer val() {
        return this.val;
    }

    String name() {
        return this.name;
    }

    @Override
    public String toString() {
        return "ValObj{" +
                "name='" + name + '\'' +
                ", val=" + val +
                '}';
    }
}
